# 2025年2月20日10:19:28
# 将收集到的几个数据集合并，然后清除重复的



import os
import hashlib
from PIL import Image

def get_image_hash(image_path):
    try:
        # 打开图片
        image = Image.open(image_path)
        # 将图片转换为字节流
        image_bytes = image.tobytes()
        # 计算哈希值
        hash_object = hashlib.sha256(image_bytes)
        return hash_object.hexdigest()
    except Exception as e:
        print(f"Error processing {image_path}: {e}")
        return None

def find_duplicate_images(folder_path):
    hash_dict = {}
    # 遍历文件夹中的所有文件
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            file_path = os.path.join(root, file)
            # 检查文件是否为图片
            if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
                # 计算图片的哈希值
                image_hash = get_image_hash(file_path)
                if image_hash:
                    if image_hash in hash_dict:
                        hash_dict[image_hash].append(file_path)
                    else:
                        hash_dict[image_hash] = [file_path]

    # 找出重复的图片
    duplicate_images = {k: v for k, v in hash_dict.items() if len(v) > 1}
    return duplicate_images

# 根据重复名称删掉图片和标注
def delete_duplicate_images_and_labels(duplicates):
    for hash_value, paths in duplicates.items():
        # 保留第一张图片，删除其余重复图片及其标注
        for path in paths[1:]:
            try:
                # 删除图片文件
                os.remove(path)
                print(f"已删除图片: {path}")

                # 获取标注文件路径
                base_dir = os.path.dirname(os.path.dirname(path))
                label_dir = os.path.join(base_dir, 'labels')
                file_name = os.path.basename(path)
                label_name = os.path.splitext(file_name)[0] + '.txt'
                label_path = os.path.join(label_dir, label_name)

                if os.path.exists(label_path):
                    # 删除标注文件
                    os.remove(label_path)
                    print(f"已删除标注文件: {label_path}")
            except Exception as e:
                print(f"删除 {path} 时出错: {e}")

if __name__ == "__main__":
    folder_path = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\washed"  # 替换为实际的文件夹路径
    duplicates = find_duplicate_images(folder_path)
    delete_duplicate_images_and_labels(duplicates)